﻿using System;

namespace MailClient.Data
{
    public static class Extensions
    {
        public static T[] CloneSubArray<T>(this T[] data, int index, int length)
where T : ICloneable
        {
            if (length <= 0)
            { return data; }
            else
            {
                T[] result = new T[length];
                for (int i = 0; i < length; i++)
                {
                    var original = data[index + i];
                    if (original != null)
                        result[i] = (T)original.Clone();
                }
                return result;
            }
        }

        public static string Word(this string query, int start, string separator)
        {
            string result = "";
            if (start >= query.Length)
            { return " "; }
            query = query.Remove(0, start);
            int pos = query.IndexOf(separator);
            if (pos <= 0)
            { return query; }
            else
            {
                for (int i = start; i <= pos; i++)
                {
                    result += query[i];
                }
                return result;
            }
        }

        /// <summary>
        /// Determines whether the source string contains the string value at some point. Comparison specifics
        /// can be passed in to allow for culture specific comparisons.
        /// </summary>
        /// <param name="source">The source string in which to search.</param>
        /// <param name="value">The string value to look for.</param>
        /// <param name="comparison">Specifies the type of search to use for the specified string.</param>
        /// <returns>
        ///  <c>true</c> if the specified source string contains the value string; otherwise, <c>false</c>.
        /// </returns>
        /// <remarks>Ideal for supporting multi-language and culture specific searches.</remarks>
        public static bool Contains(this string source, string value, StringComparison comparison)
        {
            // Perform the comparison specific search.
            if (value == null)
            {
                return false;
            }
            return source.IndexOf(value, comparison) >= 0;
        }




    }
}
